function logon() {
    this.callSuper();
    this.configuration = {
        "i18n": "nls/logon",
        "apis": "../../config/apis/logon"
    };
    this._viewIndex = "home";

    // global variables
    this.account = "";
}

logon.prototype.homeHasParsed = function() {
    var self = this,
        windowHeight = window.innerHeight;

    // event binding
    self.registerSwipeGesture("home", function() {
        self.goTo("logon");
    });
};

logon.prototype.registerSwipeGesture = function(element, callback) {
    var self = this,
        startPos, endPos, point,
        supportTouch = "ontouchstart" in window,
        START_EV = "mousedown", MOVE_EV = "mousemove", END_EV = "mouseup mouseleave",
        isStart = false;

    // check if support touch event
    if (supportTouch) {
        START_EV = "touchstart";
        MOVE_EV = "touchmove";
        END_EV = "touchend touchcancel";
    }

    // convert to jQuery object if "element" is object id instead of object
    if (typeof element === "string") {
        element = $("#" + element);
    } else if (typeof element === "object" && element.length === undefined) {
        element = $(element);
    }

    if (element.length > 0) {
        element.on(START_EV, function(e) {
            e.preventDefault();
            isStart = true;
            point = e.originalEvent.touches ? e.originalEvent.touches[0] : e;
            startPos = point.pageX;
        // move the switch button when mouse or touch moving
        }).on(MOVE_EV, function(e) {
            e.preventDefault();
            if (isStart) {
                point = e.originalEvent.touches ? e.originalEvent.touches[0] : e;
                endPos = point.pageX;
            }
        // event fired when mouse move out of the target element
        // or too many touch points
        }).on(END_EV, function(e) {
            e.preventDefault();
            if (isStart) {
                isStart = false;
                if (endPos - startPos < 0) {
                    callback.call(self);
                }
            }
        });
    }
};

logon.prototype.logonHasParsed = function() {
    var self = this;

    // event binding
    iData.get("logonInputs").on("input", "input", function() {
        // mandatory checking
        self.checkUsernamePassword();
    });

    iData.get("btnGo").click(function() {
        if (!$(this).hasClass("disabled")) {
            // send logon request
            self.request({
                name: "logonAccount",
                method: "POST",
                data: self.logonRequestParam,
                callback: self.logonCallback
            });
        }
    });

    iData.get("to_register").click(function() {
        self.goTo("registration");
    });
};

logon.prototype.checkUsernamePassword = function() {
    var self = this,
        username = $.trim(iData.get("username").val()),
        // password = $.trim(iData.get("password").val()),
        mobileNumber = $.trim(iData.get("mobileNumber").val()),
        $btnGo = iData.get("btnGo");

    // dim button is either username or password is blank
    // if (username && password) {
    if (username && mobileNumber) {
        $btnGo.removeClass("disabled");
    } else {
        $btnGo.addClass("disabled");
    }
};

logon.prototype.logonRequestParam = function() {
    var self = this,
        // get value of mandatory fields
        username = $.trim(iData.get("username").val()),
        // password = $.trim(iData.get("password").val());
        mobileNumber = $.trim(iData.get("mobileNumber").val());

    // save account to variable
    self.account = username;

    return {
        account: username,
        // password: password
        mobileNumber: mobileNumber
    };
};

logon.prototype.logonCallback = function() {
    var self = this;

    return {
        "success": function(respData) {
            var data = respData.data,
                body = data.body,
                secureToken = body.secureToken,
                // keyNames for setter
                keyNames = self.getCustomerInfoKeys(),
                // data object
                customerData = {};

            // validate secure token
            if (secureToken) {
                self.validateSecureToken(secureToken, function(isValidToken) {
                    if (!isValidToken) {
                        // stop logon process if secure token is not valid
                        self.goBack("home");
                        return;
                    }
                });
            }

            customerData[keyNames.account] = self.account;
            customerData[keyNames.fullName] = body.fullName;
            customerData[keyNames.company] = body.company;
            customerData[keyNames.location] = body.location;
            customerData[keyNames.secureToken] = secureToken || "";
            customerData[keyNames.sponsorCode] = body.sponsorCode || "";

            // save data to local db
            self.setCustomerInfo(customerData, function() {
                self.goTo("file:///web/postlogon/menu/menu.html", true);
            });
        },
        "error": function(respData) {
            var data = respData.data,
                header = data.header;

            if (header.statusCode === "E001") {
                iData.get("logonInfo").addClass("hidden");
                iData.get("warningWrapper").text(header.errorMsg[0].desc).removeClass("hidden");
                iData.get("username").val("");
                // iData.get("password").val("");
                iData.get("mobileNumber").val("");
                iData.get("btnGo").addClass("disabled");
            }
        }
    };
};

// === Register START ===
logon.prototype.registrationHasParsed = function() {
    var self = this;

    iData.get("register-inputs").on("input", "input", function() {
        self.checkRegisterInput();
    });

    iData.get("locationInput").on("click", "input", function() {
        if ($(this).is(":checked")) {
            self.location = $(this).attr("value");
            self.checkRegisterInput();
        }
    });

    iData.get("btnRegister").on("click", function() {
        if (!$(this).hasClass("disabled")) {
            self.request({
                name: "registerAccount",
                method: "POST",
                data: self.registerRequestParam,
                callback: self.registerCallback
            });
        }
    });

    iData.get("to_logon").click(function() {
        self.goBack("logon");
    });
};

logon.prototype.registerRequestParam = function() {
    var self = this,
        email = iData.get("email").val(),
        staff = iData.get("staffId").val();

    return {
        "requestName": "registerAccount",
        "account": staff,
        "email": email,
        "location": self.location
    };
};

logon.prototype.registerCallback = function() {
    var self = this;
    return {
        "success": function(respData) {
            // clean up the customer info
            iData.get("staffId").val("");
            iData.get("email").val("");
            iData.get("btnRegister").addClass("disabled");
            self.goTo("thankyou");
        },
        "error": function(respData) {
            // DO NOTHING
        }
    };
};

logon.prototype.checkRegisterInput = function() {
    var self = this,
        staff = $.trim(iData.get("staffId").val()),
        email = $.trim(iData.get("email").val()),
        $btnRegister = iData.get("btnRegister");

    if (staff && email && self.location) {
        $btnRegister.removeClass("disabled");
    } else {
        $btnRegister.addClass("disabled");
    }
};

logon.prototype.thankyouHasParsed = function() {
    var self = this;

    iData.get("back_to_home").click(function() {
        self.goBack("home");
    });
};
// === Register END ===

logon.prototype.buildModel = function() {
    var self = this;

    iData.add({
        "logo": $(".ui-logo-green-laisee"),
        "warningWrapper": $("#warningWrapper"),
        "logonInfo": $("#logonInfo"),
        // input fields
        "logonInputs": $("#logon-inputs"),
        "username": $("#username"),
        // "password": $("#password"),
        "mobileNumber": $("#mobileNumber"),
        // Go button
        "btnGo": $("#btnGo"),
        "to_register": $("#to_register"),
        "register-inputs": $("#register-inputs"),
        "btnRegister": $("#btnRegister"),
        "email": $("#email"),
        "staffId": $("#staffId"),
        "locationInput": $("#locationInput"),
        "to_logon": $("#to_logon"),
        "back_to_home": $("#back_to_home")
    });
};

logon.superclass = iFunction;
logon.start = function() {
    return new logon();
};
iClass(logon);